home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GSFILE.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  4KB  |  139 lines

  1. /* Copyright (C) 1990 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsfile.c */
  20. /* Bitmap file writing routines for Ghostscript library */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsmatrix.h"            /* for gxdevice.h */
  25. #include "gxdevice.h"
  26. #include "gxdevmem.h"
  27.  
  28. /* Dump the contents of a memory device in PPM format. */
  29. int
  30. gs_writeppmfile(gx_device_memory *md, FILE *file)
  31. {    int raster = gx_device_raster((gx_device *)md, 0);
  32.     int height = md->height;
  33.     int depth = md->color_info.depth;
  34.     uint rsize = raster * 3;    /* * 3 just for mapped color */
  35.     byte *row = (byte *)gs_malloc(rsize, 1, "ppm file buffer");
  36.     const char *header;
  37.     int y;
  38.     int code = 0;            /* return code */
  39.     if ( row == 0 )            /* can't allocate row buffer */
  40.         return_error(gs_error_VMerror);
  41.  
  42.     /* A PPM file consists of: magic number, comment, */
  43.     /* width, height, maxvalue, (r,g,b).... */
  44.  
  45.     /* Dispatch on the type of the device -- 1-bit mono, */
  46.     /* 8-bit (gray scale or color), 24- or 32-bit true color. */
  47.  
  48.     switch ( depth )
  49.        {
  50.     case 1:
  51.       header = "P4\n# Ghostscript 1 bit mono image dump\n%d %d\n";
  52.       break;
  53.  
  54.     case 8:
  55.       header = (gx_device_has_color(md) ?
  56.         "P6\n# Ghostscript 8 bit mapped color image dump\n%d %d\n255\n" :
  57.         "P5\n# Ghostscript 8 bit gray scale image dump\n%d %d\n255\n");
  58.       break;
  59.  
  60.     case 24:
  61.       header = "P6\n# Ghostscript 24 bit color image dump\n%d %d\n255\n";
  62.       break;
  63.  
  64.     case 32:
  65.       header = "P6\n# Ghostscript 32 bit color image dump\n%d %d\n255\n";
  66.       break;
  67.  
  68.     default:            /* shouldn't happen! */
  69.       code = gs_error_undefinedresult;
  70.       goto done;
  71.        }
  72.  
  73.     /* Write the header. */
  74.     fprintf(file, header, md->width, height);
  75.  
  76.     /* Dump the contents of the image. */
  77.     for ( y = 0; y < height; y++ )
  78.        {    int count;
  79.         register byte *from, *to, *end;
  80.         (*md->procs->get_bits)((gx_device *)md, y, row, NULL);
  81.         switch ( depth )
  82.            {
  83.         case 8:
  84.            {    /* Mapped color, consult the map. */
  85.             if ( gx_device_has_color(md) )
  86.                {    /* Map color */
  87.                 byte *palette = md->palette;
  88.                 from = row + raster + raster;
  89.                 memcpy(from, row, raster);
  90.                 to = row;
  91.                 end = from + raster;
  92.                 while ( from < end )
  93.                    {    register byte *cp =
  94.                       palette + (int)*from++ * 3;
  95.                     to[0] = cp[0];    /* red */
  96.                     to[1] = cp[1];    /* green */
  97.                     to[2] = cp[2];    /* blue */
  98.                     to += 3;
  99.                    }
  100.                 count = raster * 3;
  101.                }
  102.             else
  103.                {    /* Map gray scale */
  104.                 register byte *palette = md->palette;
  105.                 from = to = row, end = row + raster;
  106.                 while ( from < end )
  107.                     *to++ = palette[(int)*from++ * 3];
  108.                 count = raster;
  109.                }
  110.            }
  111.             break;
  112.         case 32:
  113.            {    /* This case is different, because we must skip */
  114.             /* every fourth byte. */
  115.             from = to = row, end = row + raster;
  116.             while ( from < end )
  117.                {    /* from[0] is unused */
  118.                 to[0] = from[1];    /* red */
  119.                 to[1] = from[2];    /* green */
  120.                 to[2] = from[3];    /* blue */
  121.                 from += 4;
  122.                 to += 3;
  123.                }
  124.             count = to - row;
  125.            }
  126.             break;
  127.         default:
  128.             count = raster;
  129.            }
  130.         if ( fwrite(row, 1, count, file) < count )
  131.            {    code = gs_error_ioerror;
  132.             goto done;
  133.            }
  134.        }
  135.  
  136. done:    gs_free((char *)row, rsize, 1, "ppm file buffer");
  137.     return code;
  138. }
  139.